<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="13">
          <el-input
            v-model="getUserListParams.query"
            placeholder="请输入内容"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="search"
            ></el-button>
          </el-input>
        </el-col>

        <el-col :span="3" :offset="1">
          <el-button @click="dialogAddFormVisible = true"> 添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="userList" border>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="电话" prop="mobile"></el-table-column>
        <el-table-column label="角色" prop="role_name"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              @change="changeStateFn(scope.row)"
            >
            </el-switch>

          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              circle
              @click="editFn(scope.row.id)"
            ></el-button>
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-setting"
              circle
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              circle
              @click="delFn(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getUserListParams.pagenum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="getUserListParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog title="添加用户" :visible.sync="dialogAddFormVisible">
      <el-form :model="addUserParams" :rules="addRules" ref="addform">
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input
            v-model="addUserParams.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="密码"
          :label-width="formLabelWidth"
          prop="password"
        >
          <el-input
            v-model="addUserParams.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="addUserParams.email" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input
            v-model="addUserParams.mobile"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetAddForm('addform')">取 消</el-button>
        <el-button type="primary" @click="addUserFn('addform')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <el-dialog title="修改用户" :visible.sync="dialogEditFormVisible">
      <el-form :model="EditUserParams">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input
            v-model="EditUserParams.username"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>

        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input
            v-model="EditUserParams.email"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input
            v-model="EditUserParams.mobile"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary" @click="editOK">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import _ from "lodash";
import {
  getusers,
  addusers,
  delusers,
  userbyid,
  editusers,
  userstate,
} from "@/http/user.js";
export default {
  name: "P102110cUsers",

  data() {
    return {
      formLabelWidth: "100px",
      userList: [{}],
      total: 0, 
      getUserListParams: {
        query: "",
        pagenum: 1,
        pagesize: 2,
      },
      dialogAddFormVisible: false,
      addUserParams: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      addRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },

      dialogEditFormVisible: false,
      EditUserParams: {
        id: "", 
        username: "", 
        email: "",
        mobile: "",
      },
    };
  },

  mounted() {
    this.getUserList();
  },

  watch: {
    userList: {
      handler(n) {
        if (this.getUserListParams.pagenum > 1 && n.length == 0) {
          this.getUserListParams.pagenum -= 1;
          this.getUserList();
        }
      },
      deep: true, 
      immediate: true, 
    },
  },

  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.getUserListParams.pagesize = val;
      this.getUserList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.getUserListParams.pagenum = val;
      this.getUserList();
    },
    async getUserList() {
      let res = await getusers(this.getUserListParams);
      console.log(res);
      this.userList = res.data.users; 
      this.total = res.data.total;
    },
    addUserFn(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await addusers(this.addUserParams);
          if (res.meta.status == 201) {
            this.dialogAddFormVisible = false;
            this.getUserList();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    resetAddForm(formName) {
      this.$refs[formName].resetFields();
    },
    
    async delFn(id) {
      let res = await delusers(id);
      this.getUserList();
    },

    search: _.debounce(function () {
      console.log("哈哈哈哈哈哈", this);
      this.getUserListParams.pagenum = 1;
      this.getUserList();
    }, 2000),

    async editFn(id) {
      this.dialogEditFormVisible = true;
      let res = await userbyid(id);
      this.EditUserParams.id = id;
      this.EditUserParams.username = res.data.username;
      this.EditUserParams.email = res.data.email;
      this.EditUserParams.mobile = res.data.mobile;
    },

    async editOK() {
      let { id, email, mobile } = this.EditUserParams;
      let res = await editusers(id, { email, mobile });
      this.dialogEditFormVisible = false;
      this.getUserList();
    },
    async changeStateFn(row) {
      let { id, mg_state } = row;
      let res = await userstate(id, mg_state);
    },
  },
};
</script>

<style lang="scss" scoped>
.el-card,
.el-table,
.el-pagination {
  margin-top: 15px;
}
</style>